// pages/pkDetail/pkDetail.js
import {Pk} from "../../model/pk"
import {PkHandle} from "../../model/pk-handle"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pk: null,
    hasRight: false,
    hasWinner: false,
    leftDetail: null,
    rightDetail: null,
    winner: null
  },
  source: null,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.source = options.source
    const pk = await Pk.getPkDetail(options.pkid)
    console.log(pk)
    const pkHandle = new PkHandle(pk)
    const hasRight = pkHandle.hasRight
    const leftDetail = pkHandle.getLeftDetail()
    const rightDetail = pkHandle.getRightDetail()
    this.setData({
      pk,
      leftDetail,
      rightDetail
    })
    if (hasRight) {
      const hasWinner = pkHandle.hasWinner()
      this.setData({
        hasWinner,
        hasRight
      })
      if (hasWinner) {
        const winner = pkHandle.getWinner()
        this.setData({
          winner
        })
      }
    }else {
      this.setData({
        hasRight,
        leftDetail
      })
    }
  },
  onKnow() {
    if (this.source === 'share') {
      wx.switchTab({
        url: '/pages/home/home'
      })
    }else {
      wx.navigateBack()
    }
  }
})
